<template>
  <div class="app-container">
    <div class="filter-container" style="display: flex;
    flex-direction: row-reverse;">
      <div class="lineBox">
        <el-form :inline="true" :model="listQuery" class="demo-form-inline">
          <el-form-item>
            <el-button size="mini" type="primary" @click="add(2)">
              新建实体关系抽取规则
            </el-button>
            <el-button size="mini" type="primary" @click="add(1)">
              新建属性抽取规则
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <div class="myTable" style="min-height: 600px;">
      <el-table :key="tableKey" v-loading="listLoading" :data="list" fit size="mini" highlight-current-row
        style="width: 100%;">
        <el-table-column label="ID" prop="id" align="center" min-width="50" />
        <el-table-column label="标题" prop="title" show-overflow-tooltip align="center" min-width="150" />
        <el-table-column label="所属模板" prop="template_name" show-overflow-tooltip align="center" min-width="150" />
        <el-table-column label="采集类型" prop="collection_type" align="center" min-width="150" />
        <el-table-column label="采集规则" show-overflow-tooltip prop="template_name" align="center" min-width="200">
          <template slot-scope="{row,$index}">
            {{ row.extract_content }}
          </template>
        </el-table-column>
        <el-table-column label="优先级" prop="priority" align="center" min-width="50" />
        <el-table-column label="启用状态" prop="status" align="center" min-width="100">
          <template slot-scope="{row,$index}">
            <div v-if="row.status== '1'">
              <span class="statusPie success" />
              <span class="text">是</span>
            </div>
            <div v-else>
              <span class="statusPie danger" />
              <span class="text">否</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" align="center" min-width="200" class-name="small-padding fixed-width">
          <template slot-scope="{row,$index}">
            <el-button type="primary" size="mini" @click="skip(row,1)">
              编辑
            </el-button>

            <el-button size="mini" type="danger" @click="handleDelete(row,$index)">
              删除
            </el-button>
            <el-button type="success" size="mini" @click="skip(row,2 )">
              复制
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination size="mini" :page-sizes="[10]" :total="total" :page.sync="listQuery.page"
        :limit.sync="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" @pagination="dataList" />
    </div>
  </div>
</template>

<script>
import { extractionrulesApi } from '@/api/extractionrules'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {

  components: { Pagination },

  data() {
    return {

      formInline: {
        user: "",
        region: ""
      },
      value: '',

      tableKey: 0,
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        pageNumber: 1,
        page: 1,
        limit: 10,

      },


    }
  },



  created() {

    this.dataList()
  },
  methods: {

    // 数据列表
    async dataList() {
      // this.listLoading = true
      const response = await extractionrulesApi("getExtractionrules", this.listQuery)
      if (response.code == 200) {
        this.list = response.data.resultList
        this.total = response.totalCount
        // this.type = response.type
        //   // Just to simulate the time of the request
        // setTimeout(() => {
        //   this.listLoading = false
        // }, 1.5 * 1000)
      }
    },

    // 删除指标
    async handleDelete(row, index) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const data = { 'id': row.id }

        const response = await extractionrulesApi("delExtractionRulesById", data)
        if (response.code == 200) {
          this.$notify({
            title: '成功',
            message: response.message,
            type: 'success',
            duration: 2000
          })
          this.list.splice(index, 1)
        } else {
          this.$notify({
            title: '失败',
            message: response.message,
            type: 'error',
            duration: 2000
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },


    add(type) {
      if (type == "1") {
        this.$router.push('/template/attributeExtractionSave')
      } else {
        this.$router.push('/template/relationshipExtractionSave')
      }
    },
    // 修改跳转
    skip(row, index) {

      if (row.collection_type == "属性抽取") {
        this.$router.push('/template/attributeExtractionSave?id=' + row.id + '&copy=' + index)
      } else {
        this.$router.push('/template/relationshipExtractionSave?id=' + row.id + '&copy=' + index)
      }



    }
  }
}
</script>

<style lang="scss" scoped>
.demo-image__preview {
  float: left;
  margin-left: 5px;
}
.demonstration {
  font-size: 12px;
  color: #333;
  font-weight: normal;
}
</style>
